{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Testing(h).ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/HasikaR/Audio-Denoising-Deep-Learning/blob/main/Testing(h).ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lsnBbMasQoA8",
        "outputId": "5eb0b8c9-5a2c-4e43-952b-f1f1c080bce9"
      },
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mounted at /content/drive\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yTY7L8PCQ-2v"
      },
      "source": [
        "import zipfile"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "03moUYgbTyFY"
      },
      "source": [
        "from keras.models import Sequential\n",
        "from keras.layers import Dense\n",
        "from keras.models import model_from_json\n",
        "import numpy\n",
        "import os"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hZ5XybADREhU"
      },
      "source": [
        "with zipfile.ZipFile('/content/drive/MyDrive/s6.zip', 'r') as zip_ref:\n",
        "    zip_ref.extractall('/tmp3')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6uv81I4SRFhq"
      },
      "source": [
        "with zipfile.ZipFile('/content/drive/MyDrive/c6.zip', 'r') as zip_ref:\n",
        "    zip_ref.extractall('/tmp4')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "T2o8bE2jUB3I"
      },
      "source": [
        "import librosa\n",
        "import numpy as np"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yfR5QuJIRLBh"
      },
      "source": [
        "#test samples\n",
        "#load clean signal filenames\n",
        "files_path3='/tmp4/c6'\n",
        "tc_files=librosa.util.find_files(files_path3, ext=['wav'])\n",
        "\n",
        "#load synthetic signal filenames\n",
        "files_path4='/tmp3/s6'\n",
        "ts_files=librosa.util.find_files(files_path4, ext=['wav'])\n",
        "\n",
        "#create a multicolumn object with synthetic and clean signal names\n",
        "tc1_arr=np.array(tc_files)\n",
        "ts1_arr=np.array(ts_files)\n",
        "t_samples= np.column_stack((tc1_arr,ts1_arr))\n",
        "test_samples=np.array(t_samples)\n",
        "#test_samples=ts1_arr"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "rc74cNw4ScWK",
        "outputId": "3617813a-7e64-44e3-d2f2-930460df9d5c"
      },
      "source": [
        "print(len(test_samples))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "36\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "e50dR-uIEJkJ"
      },
      "source": [
        "y, sr = librosa.load('/tmp3/s6/ss2221_cs75_kd1.wav', sr =None)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "a4EmnUU9EkFh",
        "outputId": "7748a428-1e71-48f6-9371-6ebe19dfd8de"
      },
      "source": [
        "print(y)\n",
        "print(y.shape)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[-0.00018311 -0.00064087 -0.00012207 ... -0.00479126  0.00082397\n",
            "  0.        ]\n",
            "(576000,)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-1a4c-gK9AqT",
        "outputId": "96e23973-7e50-4f1c-d5c4-0e23a899ee6d"
      },
      "source": [
        "print(ts1_arr)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "['/tmp3/s6/ss2221_cs75_kd1.wav' '/tmp3/s6/ss2222_cs75_kd2.wav'\n",
            " '/tmp3/s6/ss2223_cs75_kd3.wav' '/tmp3/s6/ss2224_cs75_kd4.wav'\n",
            " '/tmp3/s6/ss2225_cs75_kd5.wav' '/tmp3/s6/ss2226_cs75_kd6.wav'\n",
            " '/tmp3/s6/ss2227_cs75_kd7.wav' '/tmp3/s6/ss2228_cs75_kd8.wav'\n",
            " '/tmp3/s6/ss3481_cs42_tci1.wav' '/tmp3/s6/ss3482_cs42_tci2.wav'\n",
            " '/tmp3/s6/ss3483_cs42_tci3.wav' '/tmp3/s6/ss3484_cs42_tci4.wav'\n",
            " '/tmp3/s6/ss3485_cs42_tci5.wav' '/tmp3/s6/ss3486_cs42_tci6.wav'\n",
            " '/tmp3/s6/ss3487_cs42_tci7.wav' '/tmp3/s6/ss4531_cs2_vc1.wav'\n",
            " '/tmp3/s6/ss4532_cs2_vc2.wav' '/tmp3/s6/ss4533_cs2_vc3.wav'\n",
            " '/tmp3/s6/ss4534_cs2_vc4.wav' '/tmp3/s6/ss4535_cs2_vc5.wav'\n",
            " '/tmp3/s6/ss4536_cs2_vc6.wav' '/tmp3/s6/ss4537_cs2_vc7.wav'\n",
            " '/tmp3/s6/ss7351_cs21_acf1.wav' '/tmp3/s6/ss7352_cs21_acf2.wav'\n",
            " '/tmp3/s6/ss7353_cs21_acf3.wav' '/tmp3/s6/ss7354_cs21_acf4.wav'\n",
            " '/tmp3/s6/ss7355_cs21_acf5.wav' '/tmp3/s6/ss7356_cs21_acf6.wav'\n",
            " '/tmp3/s6/ss7357_cs21_acf7.wav' '/tmp3/s6/ss9811_cs28_r1.wav'\n",
            " '/tmp3/s6/ss9812_cs28_r2.wav' '/tmp3/s6/ss9813_cs28_r3.wav'\n",
            " '/tmp3/s6/ss9814_cs28_r4.wav' '/tmp3/s6/ss9815_cs28_r5.wav'\n",
            " '/tmp3/s6/ss9816_cs28_r6.wav' '/tmp3/s6/ss9817_cs28_r7.wav']\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bCnrpHBdRPSy",
        "outputId": "f59d4442-e28d-4975-98b9-4311ad2a85ee"
      },
      "source": [
        "#install pydub for audio slicing\n",
        "!pip install pydub"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Collecting pydub\n",
            "  Downloading https://files.pythonhosted.org/packages/a6/53/d78dc063216e62fc55f6b2eebb447f6a4b0a59f55c8406376f76bf959b08/pydub-0.25.1-py2.py3-none-any.whl\n",
            "Installing collected packages: pydub\n",
            "Successfully installed pydub-0.25.1\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "b0M3SfLQRQU4"
      },
      "source": [
        "from pydub import AudioSegment\n",
        "from pydub.utils import make_chunks\n",
        "sig_slice = []\n",
        "def audio_slicer(file_name):\n",
        "    myaudio = AudioSegment.from_file(file_name , \"wav\") \n",
        "    chunk_length_ms = 12000 # pydub calculates in millisec\n",
        "    chunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec\n",
        "        \n",
        "    #Export all of the individual chunks as wav files\n",
        "    for i, chunk in enumerate(chunks):\n",
        "        chunk_name = \"chunk{0}.wav\".format(i)\n",
        "        chunk.export(chunk_name, format=\"wav\")\n",
        "        data,sr = librosa.load(chunk_name, sr=None)\n",
        "        sig_slice.append(data)\n",
        "    return sig_slice"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EqVvCc6TRUjs"
      },
      "source": [
        "def generator(samples, batch_size):\n",
        "  num_samples = len(samples)\n",
        "  while True:\n",
        "      for offset in range(0, num_samples, batch_size):\n",
        "            # Get the samples you'll use in this batch\n",
        "            batch_samples = samples[offset:offset+batch_size]\n",
        " \n",
        "            # Initialise X_train and y_train arrays for this batch\n",
        "            X_train = []\n",
        "            y_train = []\n",
        "            count = 0\n",
        "\n",
        "            # For each example\n",
        "            for _ in batch_samples:\n",
        "                # Load image (X)\n",
        "                i=0;\n",
        "                count += 12\n",
        "                syn_filename = samples[i][0]\n",
        "                sliced_syn_signal = audio_slicer(syn_filename)\n",
        "                        \n",
        "                X_train.append(sliced_syn_signal)\n",
        "                # Read label (y)\n",
        "               \n",
        "                clean_filename = samples[i][1]\n",
        "                sliced_cln_signal = audio_slicer(syn_filename)\n",
        "                y_train.append(sliced_cln_signal)\n",
        "                print(y_train)\n",
        "                \n",
        "                if count >= batch_size:\n",
        "                    # Make sure they're numpy arrays (as opposed to lists)\n",
        "                    X_train = np.asarray(X_train)\n",
        "                    #X_train = X_train.reshape(1,48000,1)\n",
        "                    \n",
        "                    y_train = np.array(y_train)\n",
        "                    #y_train = y_train.reshape(1,48000,1)\n",
        "                    #print(X_train.shape)\n",
        "                    #print(y_train.shape)\n",
        "                    # The generator-y part: yield the next training batch            \n",
        "                    yield X_train, y_train\n",
        "\n",
        "                    #clear data \n",
        "                    X_train = []\n",
        "                    y_train = []\n",
        "                    count = 0"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zHE8EC-NRX3K"
      },
      "source": [
        "test_generator = generator(test_samples, batch_size=36)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "V7i3S6iYMwP-",
        "outputId": "c10ec0a1-c1a7-48af-a13c-37848120a74c"
      },
      "source": [
        "json_file = open('/content/drive/MyDrive/model.json', 'r')\n",
        "loaded_model_json = json_file.read()\n",
        "json_file.close()\n",
        "loaded_model = model_from_json(loaded_model_json)\n",
        "# load weights into new model\n",
        "loaded_model.load_weights(\"/content/drive/MyDrive/model.h5\")\n",
        "print(\"Loaded model from disk\")\n",
        " \n"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Loaded model from disk\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "eGOSYedeRwmt",
        "outputId": "07caf411-447c-4054-c086-569afdbe2cec"
      },
      "source": [
        "loaded_model.summary()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Model: \"sequential\"\n",
            "_________________________________________________________________\n",
            "Layer (type)                 Output Shape              Param #   \n",
            "=================================================================\n",
            "lstm (LSTM)                  (None, None, 16)          33869888  \n",
            "_________________________________________________________________\n",
            "time_distributed (TimeDistri (None, None, 529200)      8996400   \n",
            "=================================================================\n",
            "Total params: 42,866,288\n",
            "Trainable params: 42,866,288\n",
            "Non-trainable params: 0\n",
            "_________________________________________________________________\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IGUpdTQzmteW"
      },
      "source": [
        "#loaded_model.compile(loss='mean_squared_error', optimizer='adam')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QATTf-d1hNVc"
      },
      "source": [
        "import keras"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rjdj0J8SUoS-"
      },
      "source": [
        "from keras.callbacks import EarlyStopping, ModelCheckpoint\n",
        "from keras.layers import TimeDistributed\n",
        "loaded_model.compile(loss='mean_squared_error', optimizer='adam')\n",
        "mycall = [EarlyStopping(monitor='loss', min_delta=0.001, patience=1, mode='min', verbose=1),ModelCheckpoint(filepath=\"/content/drive/MyDrive/weights.hdf5\", monitor = 'loss', verbose=1, save_best_only=True,mode='min', save_freq = 1)]"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DqbhDDVYmJGw"
      },
      "source": [
        "class LossHistory(keras.callbacks.Callback):\n",
        "    def on_train_begin(self, logs={}):\n",
        "        self.history = {'loss':[],'val_loss':[]}\n",
        "\n",
        "    def on_batch_end(self, batch, logs={}):\n",
        "        self.history['loss'].append(logs.get('loss'))\n",
        "\n",
        "    def on_epoch_end(self, epoch, logs={}):\n",
        "        self.history['val_loss'].append(logs.get('val_loss'))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aqRJUMQfhD8q"
      },
      "source": [
        "class LossAndErrorPrintingCallback(keras.callbacks.Callback):\n",
        "    def on_train_batch_end(self, batch, logs=None):\n",
        "        print(\"For batch {}, loss is {:7.2f}.\".format(batch, logs[\"loss\"]))\n",
        "\n",
        "    def on_test_batch_end(self, batch, logs=None):\n",
        "        print(\"For batch {}, loss is {:7.2f}.\".format(batch, logs[\"loss\"]))\n",
        "\n",
        "    def on_epoch_end(self, epoch, logs=None):\n",
        "        print(\n",
        "            \"The average loss for epoch {} is {:7.2f} \"\n",
        "            \"and mean absolute error is {:7.2f}.\".format(\n",
        "                epoch, logs[\"loss\"], logs[\"mean_absolute_error\"]\n",
        "            )\n",
        "        )"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rY456MBRQBOR"
      },
      "source": [
        "import tensorflow as tf\n",
        "import gc\n",
        "\n",
        "class MyCustomCallback(tf.keras.callbacks.Callback):\n",
        "    def on_epoch_end(self, epoch, logs=None):\n",
        "        gc.collect()\n",
        "        keras.backend.clear_session()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "uyOxzZtmhJzz",
        "outputId": "97d6de76-c8d7-452e-d9b0-d6056b793f43"
      },
      "source": [
        "#predictions\n",
        "ptest =loaded_model.predict(test_generator, steps = len(test_samples)//36,callbacks=[LossHistory(),MyCustomCallback()], workers=1,verbose=1)\n",
        "print(ptest)\n"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[[array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32)]]\n",
            "[[array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32)], [array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32)]]\n",
            "[[array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32)], [array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32)], [array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32)]]\n",
            "[[array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32)]]\n",
            "[[array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32)], [array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32)]]\n",
            "[[array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32)], [array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32)], [array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32), array([-3.0517578e-05, -1.2207031e-04,  0.0000000e+00, ...,\n",
            "       -1.2207031e-03,  9.1552734e-05,  5.4931641e-04], dtype=float32)]]\n",
            "1/1 [==============================] - 1s 1s/step\n",
            "[[[-1.8788967e-05 -4.1040615e-04 -2.2808410e-04 ... -2.3900915e-03\n",
            "   -2.5738536e-03 -2.5246204e-03]\n",
            "  [-1.0847943e-04 -4.7882367e-04 -3.6462478e-04 ... -2.2640461e-03\n",
            "   -2.5047925e-03 -2.5818616e-03]\n",
            "  [-1.9168766e-04 -5.3034164e-04 -4.6861614e-04 ... -2.1623389e-03\n",
            "   -2.4527772e-03 -2.6223075e-03]\n",
            "  [-2.6705209e-04 -5.6887697e-04 -5.4691517e-04 ... -2.0814203e-03\n",
            "   -2.4133627e-03 -2.6503818e-03]\n",
            "  [-3.3406305e-04 -5.9738092e-04 -6.0542877e-04 ... -2.0178105e-03\n",
            "   -2.3832978e-03 -2.6694203e-03]\n",
            "  [-3.9282121e-04 -6.1812438e-04 -6.4895564e-04 ... -1.9683316e-03\n",
            "   -2.3602159e-03 -2.6819140e-03]]\n",
            "\n",
            " [[-1.8788967e-05 -4.1040615e-04 -2.2808410e-04 ... -2.3900915e-03\n",
            "   -2.5738536e-03 -2.5246204e-03]\n",
            "  [-1.0847943e-04 -4.7882367e-04 -3.6462478e-04 ... -2.2640461e-03\n",
            "   -2.5047925e-03 -2.5818616e-03]\n",
            "  [-1.9168766e-04 -5.3034164e-04 -4.6861614e-04 ... -2.1623389e-03\n",
            "   -2.4527772e-03 -2.6223075e-03]\n",
            "  [-2.6705209e-04 -5.6887697e-04 -5.4691517e-04 ... -2.0814203e-03\n",
            "   -2.4133627e-03 -2.6503818e-03]\n",
            "  [-3.3406305e-04 -5.9738092e-04 -6.0542877e-04 ... -2.0178105e-03\n",
            "   -2.3832978e-03 -2.6694203e-03]\n",
            "  [-3.9282121e-04 -6.1812438e-04 -6.4895564e-04 ... -1.9683316e-03\n",
            "   -2.3602159e-03 -2.6819140e-03]]\n",
            "\n",
            " [[-1.8788967e-05 -4.1040615e-04 -2.2808410e-04 ... -2.3900915e-03\n",
            "   -2.5738536e-03 -2.5246204e-03]\n",
            "  [-1.0847943e-04 -4.7882367e-04 -3.6462478e-04 ... -2.2640461e-03\n",
            "   -2.5047925e-03 -2.5818616e-03]\n",
            "  [-1.9168766e-04 -5.3034164e-04 -4.6861614e-04 ... -2.1623389e-03\n",
            "   -2.4527772e-03 -2.6223075e-03]\n",
            "  [-2.6705209e-04 -5.6887697e-04 -5.4691517e-04 ... -2.0814203e-03\n",
            "   -2.4133627e-03 -2.6503818e-03]\n",
            "  [-3.3406305e-04 -5.9738092e-04 -6.0542877e-04 ... -2.0178105e-03\n",
            "   -2.3832978e-03 -2.6694203e-03]\n",
            "  [-3.9282121e-04 -6.1812438e-04 -6.4895564e-04 ... -1.9683316e-03\n",
            "   -2.3602159e-03 -2.6819140e-03]]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5bmXJXQVfWL-",
        "outputId": "37500028-47e1-41b7-df79-4f0b26d9357d"
      },
      "source": [
        "print(type(ptest))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "<class 'numpy.ndarray'>\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LJBbh7GohAgN"
      },
      "source": [
        "history = LossHistory()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "B5em6Lyct11d",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "c536e49c-16db-4231-9fd1-615dbf4d50db"
      },
      "source": [
        "print(ptest.shape)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(3, 72, 44100)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EF8jlThelTy0",
        "outputId": "e23e5ff1-19aa-4ee1-e2e0-3708d45841bf"
      },
      "source": [
        "#%store -r numpy_loss_history"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "no stored variable numpy_loss_history\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "dGZUhVbhXJZ6",
        "outputId": "6c295cf4-aac6-43dc-a9cd-abd2e4fe5a46"
      },
      "source": [
        "\n",
        "#convert numpy array to audio file\n",
        "from pydub import AudioSegment as export\n",
        "import numpy as np\n",
        "import librosa\n",
        "import soundfile as sf\n",
        "from scipy.io.wavfile import write\n",
        "\n",
        "from pydub.playback import play\n",
        "from IPython.display import Audio\n",
        "xx=[]\n",
        "\n",
        "for j, sig in enumerate(ptest[0]):\n",
        "    pt1 = \"sig{0}.wav\".format(j)\n",
        "    print(sig[0])\n",
        "    xx.append(sig[0])\n",
        "    fs = 44100\n",
        "    #sd.play(sig, fs)\n",
        "\n",
        "    #sig.export('/content/drive/MyDrive/lstm_output/predictoutputpt1'+pt1, format=\"wav\")\n",
        "    write('/content/drive/MyDrive/lstm_output3/predictoutputpt2'+pt1,fs,sig)\n",
        "    "
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "-1.8788967e-05\n",
            "-0.00010847943\n",
            "-0.00019168766\n",
            "-0.0002670521\n",
            "-0.00033406305\n",
            "-0.0003928212\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "xrAy9wxoGAWt",
        "outputId": "882c0da6-bc9c-4c27-938c-7759fe93226f"
      },
      "source": [
        "print(xx)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[-0.009047418, -0.009631066, -0.00913505, -0.009571094, -0.007971369, -0.00877447, -0.008882075, -0.00957768, -0.009071277, -0.009532358, -0.0079289125, -0.008763558, -0.008869739, -0.009574869, -0.009059534, -0.009519059, -0.007913397, -0.00875614, -0.008861436, -0.009568427, -0.009052487, -0.009511639, -0.0079064965, -0.008751753, -0.008857286, -0.0095650675, -0.009049422, -0.009508866, -0.007904173, -0.008750099, -0.008855854, -0.009563948, -0.009048439, -0.009508102, -0.007903577, -0.008749636]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0oaosjbtK5Hz",
        "outputId": "bba8cbc8-e7ff-4199-a6f3-fa44c84a5efd"
      },
      "source": [
        "\n",
        "from pydub import AudioSegment\n",
        "\n",
        "sound1 = AudioSegment.from_file(\"/content/drive/MyDrive/lstm_output3/predictoutputpt2sig0.wav\", format=\"wav\")\n",
        "\n",
        "# sound1 6 dB louder\n",
        "louder = sound1 + 6\n",
        "\n",
        "# simple export\n",
        "louder.export(out_f = \"/content/drive/MyDrive/lstm_output/output19.wav\", format=\"wav\")\n"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<_io.BufferedRandom name='/content/drive/MyDrive/lstm_output/output19.wav'>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 26
        }
      ]
    }
  ]
}